//
//  EWEReportSignView.swift
//  EWEMdt3
//
//  Created by 来 on 2019/11/25.
//  Copyright © 2019 laiguofeng. All rights reserved.
//

import UIKit

class EWEReportSignView: UIView {
    
    var centerView: UIView!
    var textField: UITextField!
    
    var report: ((String) -> Void)?
    
    class func lgf_Show(_ block: ((String) -> Void)?) -> Void {
        let view = EWEReportSignView()
        view.report = block
        let SV = UIApplication.shared.keyWindow!
        view.frame = SV.bounds
        SV.addSubview(view)
        view.initUI()
        UIView.animate(withDuration: 0.3) {
            view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        }
    }
    
    func initUI() {
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(_ :)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
        
        centerView = UIView()
        centerView.backgroundColor = EWEDarkMode.white()
        centerView.clipsToBounds = true
        centerView.layer.cornerRadius = 3
        addSubview(centerView)
        centerView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalToSuperview().offset(-30.lgf_W())
            make.height.equalTo(60.lgf_H())
        }
        
        textField = UITextField()
        textField.placeholder = "请输入签名密码"
        centerView.addSubview(textField)
        textField.keyboardType = .emailAddress
        textField.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(15.lgf_W())
            make.width.equalToSuperview().multipliedBy(0.7)
        }
        
        let button = UIButton.init(type: .custom)
        button.setTitle("签名", for: .normal)
        button.backgroundColor = UIColor.init(lgf_HexString: "0076FF")
        button.setTitleColor(EWEDarkMode.white(), for: .normal)
        button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        centerView.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.left.equalTo(textField.snp.right).offset(15.lgf_H())
        }
        
        textField.becomeFirstResponder()
    }
    
    @objc func buttonClick(_ sender: UIButton) {
        if textField.text?.lgf_RemoveBlank().count == 0 {
            SVProgressHUD.showError("请输入签名密码")
        } else {
            report?(textField.text ?? "")
            remove()
        }
    }
    
    @objc func keyBoardWillShow(_ notification: Notification) {
        DispatchQueue.main.async {
            let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
            let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
            UIView.animate(withDuration: duration) {
                self.centerView.transform = CGAffineTransform.init(translationX: 0, y: -(endFrame.size.height / 2.0) + 30.0)
            }
        }
    }
    
    @objc func keyBoardWillHide(_ notification: Notification) {
        DispatchQueue.main.async {
            let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
            UIView.animate(withDuration: duration) {
                self.centerView.transform = CGAffineTransform.identity
            }
        }
    }
    
    func remove() {
        self.endEditing(true)
        UIView.animate(withDuration: 0.3, animations: {
            self.backgroundColor = UIColor.black.withAlphaComponent(0.0)
            self.centerView.alpha = 0.0
            self.centerView.transform = CGAffineTransform.identity
        }) { (finish) in
            NotificationCenter.default.removeObserver(self)
            self.removeFromSuperview()
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first?.view == self {
            remove()
        }
    }
    
}
